package com.yonyou.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Component;

import com.yonyou.domain.PresetWinning;
import com.yonyou.domain.User;
import com.yonyou.service.extend.UserServiceInterfaceClient;
import com.yonyou.status.CommnonStatus;


@Component
public class PresetWinningDAO {
	@Autowired
	public MongoTemplate mongoTemplate;

	@Autowired
	public GridFsTemplate gridFsTemplate;
	
	
	@Autowired
	public UserServiceInterfaceClient userServiceInterfaceClient;

	/**
	 * 查询内定中将人员列表
	 * 
	 * @return
	 */
	public PresetWinning list(String  prize_id) {
		Criteria criteria = Criteria.where("prize_id").is(prize_id);
		criteria.andOperator(Criteria.where("dr").is(CommnonStatus.SAVSTATUS));
		Query query = Query.query(criteria);
		PresetWinning presetWinning =  mongoTemplate.findOne(query, PresetWinning.class);
		List<String> user_id = presetWinning.getUser_id();
		List<User> userlist = new ArrayList<>();
		for (Iterator<String> iterator = user_id.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			User user  = (User) userServiceInterfaceClient.findUserById(string).getData();
			userlist.add(user);
		}
		presetWinning.setUserlist(userlist);
		return presetWinning;
	}

	/**
	 * 新增内定中将人员列表(先删除后增加)
	 * @param prize
	 */
	public void save(PresetWinning presetWinning) {
		Criteria criteria = Criteria.where("prize_id").is(presetWinning.getPrize_id());
		Query query = Query.query(criteria);
		mongoTemplate.remove(query, PresetWinning.class);
		mongoTemplate.insert(presetWinning);
	}

}
